trigger update_owner_hcp on HCP__c (before insert) {
try{
    List<Hospital__c> hos1_list = new List<Hospital__c>();
    List<ID> id_list = new List<ID>();
    List<HCP__c> hcp_all = new List<HCP__c>();
    
    for(HCP__c hcp1:trigger.new)
    {
        id_list.add(hcp1.Hospital__c);
        hcp_all.add(hcp1);
    }
    List<Hospital__c> hos_all = [Select id,Name,OwnerId From Hospital__c Where id in: id_list];

    Integer num = 0;
    List<HCP__c> hcp_save = new List<HCP__c>();
       
    for(HCP__c hcp:hcp_all)
    {
        for(Hospital__c hos : hos_all){
            if(hcp.Hospital__c == hos.Id){
                hcp.hcp_owner__c = hos.OwnerId;
            }        
        }
    
        hcp_save.add(hcp);
        num++;
    }
}
    catch (DmlException e) {
        ApexPages.addMessages(e);

    }
            
}